import {HvigorTask, HvigorTaskContext} from "@ohos/hvigor";
import fs from "fs-extra";
import json5 from "json5"

interface StringItem {
    name: string,
    value: string
}

export class ScanTask implements HvigorTask {

    private static bundleName: string

    private static isLog: boolean = false

    constructor(bundleName: string, isLog: boolean) {
        ScanTask.bundleName = bundleName;
        ScanTask.isLog = isLog
    }

    name: string = "ScanTask";

    static readonly etsSuffix = ".ets"

    static readonly srcPath = "src/main/"

    static readonly etsPath = ScanTask.srcPath + "ets/";

    static readonly resourcesPath = ScanTask.srcPath + "resources/base/";

    static readonly stringPath = ScanTask.resourcesPath + "element/hrouter.json";

    static readonly pagesPath = ScanTask.resourcesPath + "profile/main_pages.json";

    static readonly modulePath = ScanTask.srcPath + "module.json5";

    run(task: HvigorTaskContext) {
        ScanTask.isLog && console.info("start scan")

        function addRouter(rootPath: string, path: string): string | null {
            let rawPath = rootPath + "/" + ScanTask.etsPath + path + ScanTask.etsSuffix

            ScanTask.isLog && console.info("scan file:" + rawPath)
            let content = fs.readFileSync(rawPath, 'utf-8')
            let res = content.match(/(?<=@Page\()\{.+}(?=\))/)
            ScanTask.isLog && console.info("scan result:" + res)
            if (res) {
                let annotation = res[0]
                let routeName = json5.parse(annotation)
                if (routeName != null) {
                    return "hrouter_1_" + routeName.name.replace(/\//g, "_1_")
                }
            }
            return null
        }

        //主module的路由扫描
        let mainPages = fs.readJsonSync(task.modulePath + "/" + ScanTask.pagesPath)
        let strings: StringItem[] = []
        ScanTask.isLog && console.info("scan->" + task.moduleName)
        ScanTask.isLog && console.info("scan->" + JSON.stringify(mainPages))
        mainPages.src.forEach((value: string) => {
            let result = addRouter(task.modulePath, value)
            if (result) {
                strings.push({
                    name: result,
                    value: "@bundle:" + ScanTask.bundleName + "/" + task.moduleName + "/ets/" + value
                })
            }
        })
        let modules = fs.readdirSync(task.modulePath + "/oh_modules")
        ScanTask.isLog && console.info("scan child module->" + JSON.stringify(modules))
        modules.forEach((value: string) => {
            let currentModulePath = task.modulePath + "/oh_modules/" + value + "/" + ScanTask.modulePath
            ScanTask.isLog && console.info("scan module:" + currentModulePath)
            if (fs.existsSync(currentModulePath)) {
                let module = json5.parse(fs.readFileSync(currentModulePath,{encoding:'utf-8'}))
                ScanTask.isLog && console.info("scan module name->" + module.module.name)
                if (module.module.type == "shared") {//只处理HSP
                    let modulePages = fs.readJsonSync(task.modulePath + "/oh_modules/" + value + "/" + ScanTask.pagesPath)
                    modulePages.src.forEach((item: string) => {
                        let result = addRouter(task.modulePath + "/oh_modules/" + value, item)
                        if (result) {
                            strings.push({
                                name: result,
                                value: "@bundle:" + ScanTask.bundleName + "/" + module.module.name + "/ets/" + item
                            })
                        }
                    })
                }
            }
        })
        ScanTask.isLog && console.info("scan count:" + strings.length)
        ScanTask.isLog && console.info("scan details:" + JSON.stringify(strings))
        if (strings.length > 0)
            fs.writeFileSync(task.modulePath + "/" + ScanTask.stringPath, JSON.stringify({string: strings}, null, 2))
    }


    dependencies = ['default@PreBuild']


    postDependencies = ['default@ProcessResource']


}